<?php
/*
 * [佳喜网络!] (C)2008-2009 JiaXi Inc.
 * $Id: include.php 2008-8-13 19:28:02 pepsi.zh $
 * -------------------------------------------
 * 网址：www.he88.cn
 * E-mail：pepsi_zh AT yeah.net
 * -------------------------------------------
 * 这是一个共享软件，遵循我们公司的共享协议。
*/
interface WebDbInter{
	### 功能：用户登陆判断  参数：$post 记录值；#####
    function addRecord($post);
    ### 功能：get网站列表for SELECT 参数：$title_long title字段的长度;
    function getRecord($userid,$title_long);
    ### 功能：得到一条记录; 参数：$id 记录id; $type 操作类型，当为ture时，必须带$userid参数 ######
    function getOneRecord($id,$type=false,$userid='');
    ### 功能：删除一条网页记录 ; 参数：$id 记录id 可以是一条数据也可以是id数组；$type 操作类型，当为ture时，必须带$userid参数 ######
    function deleteRecord($id,$type=false,$userid='');
}
class web implements WebDbInter {
	public $error = array();
	public $action = '提交';
	function addRecord($post){
		global $dbtable,$dbconn;
		$flag = true;
		$var = array();
		$post = specialhtml($post,'zcontent');
		/////得到网站的域名
		if (empty($post[zsite])){
			$flag = false;
			$this->error[zsite_error] = '站点不能为空';
		}else{
			$var[zsite] = $post[zsite];
			$query = "select zurl from $dbtable->site where zsiteid=$post[zsite] and zcheck='y' and zdelete='n' limit 1";
			$siteurl = $dbconn->Getonefield($query);
			$siteurl = preg_replace('/^www./i','',$siteurl);
		}
		if (empty($post[ztypeid])){
			$flag = false;
			$this->error[typeid_error] = '内容类型不能为空';
		}else {
			$var[ztypeid] = $post[ztypeid];
		}
		$varlongth = strlen($post[zurl]);
		if($varlongth < 3){
			$flag = false;
			$this->error[zurl_error] = '网站地址有误';
		}elseif ($varlongth > 100){
			$flag = false;
			$this->error[zurl_error] = '网站地址长度不能超过100字节';
		}else {
			$siteurl;
			$post[zurl] = getDomain($post[zurl]);
			if (strpos($post[zurl],$siteurl) === false){
				$flag = false;
				$this->error[zurl_error] = "网站地址不正确,必须是'".$siteurl."'域名下的网页";
			}else{
				$var[zurl] = $post[zurl];
			}
		}
		if (empty($post[ztitle])){
			$flag = false;
			$this->error[ztitle_error] = '标题不能为空';
		}else {
			$var[ztitle] = $post[ztitle];
		}
		$varlongth = strlen($post[zcontent]);
		if ($varlongth < 100) {
			$flag = false;
			$this->error[zcontent_error] = '网站内容不能小于10字节';
		}elseif ($varlongth > 8000){
			$flag = false;
			$this->error[zcontent_error] = '网站内容不能多余2000字节';
		}else {
			$var[zcontent] = $post[zcontent];
		}
		if (isset($post[zheadtitle])){
			$var[zheadtitle] = $post[zheadtitle];
		}
		if (isset($post[zheadkeyword])){
			$var[zheadkeyword] = $post[zheadkeyword];
		}
		if (isset($post[zheaddescrib])){
			$var[zheaddescrib] = $post[zheaddescrib];
		}
		if(isset($post[ztag])){
			$var[ztag] = $post[ztag];
		}
		if ($flag){
			$var[zdate] = time();
			$var[zkeyword] = $post[zkeyword];
			$var[ztag] = $post[ztag];
			$var[zcheck] = 'n';
			$var[zstatic] = 'n';
			if ($post[zwebid]){
				$this->action = '修改';
				$i = 1;
				foreach($var as $key =>$value){
					$key = 'a.'.$key;
					if($i==1){
						$up_date = $key."='".$value."'";
					}else{
						$up_date .= ",".$key."='".$value."'";
					}
					$i++;
				}
				$query = "update $dbtable->web a left join $dbtable->site b on(a.zsite=b.zsiteid) set $up_date where b.zuserid=$post[userId] and a.zwebid=$post[zwebid]";
				return $dbconn->Query($query);
			}else {
				$this->action = '添加';
				return $dbconn->addData($var,$dbtable->web);
			}
		}else {
			return false;
		}
	}
	function getRecord($userid,$title_long){
		global $dbtable,$dbconn,$ArticleType;
		if (empty($userid)){
			return false;
		}
		$array = array();
		$i = 1;
		$query = "select a.*,b.zname as zsitename from $dbtable->web a left join $dbtable->site b on (a.zsite=b.zsiteid) where b.zuserid=$userid order by a.zwebid desc";
		$dbconn->Query($query);
		while (($row = $dbconn->nextRecord())){
			$row[num] = $i;
			$row[zdate] = date('Y-m-d H:i:s',$row[zdate]);
			$row[ztitle_sub] = cut_str($row[ztitle],$title_long);
			$row[ztypename] = $ArticleType[$row[ztypeid]];
			$array[] = $row;
			$i++;
		}
		return $array;
	}
	function getOneRecord($id,$type=false,$userid=''){
		global $dbtable,$dbconn;
		if (empty($id)){
			return false;
		}else{
			$where = "where a.zwebid=$id";
		}
		if ($type){
			if (empty($userid)){
				return false;
			}else {
				$where .= " and b.zuserid=$userid";
			}
		}
		$query = "select a.* from $dbtable->web a left join $dbtable->site b on(a.zsite=b.zsiteid) $where limit 1";
		return $dbconn->Getonerecord($query);
	}
	function deleteRecord($id,$type=false,$userid=''){
		global $dbtable,$dbconn;
		if (empty($id)){
			return false;
		}else {
			$where_id = "a.zsiteid in (".explode(',',$id).")";
		}
		if ($type){
			if (empty($userid)){
				return false;
			}else {
				$where_id .= " and b.zuserid=$userid";
			}
		}
		$query = "delete from $dbtable->web a left join $dbtable->site b on(a.zsite=b.zsiteid) $where_id";
		$dbconn->Query($query);
		return $dbconn->returnAffected();
	}
}

?>